/**
 * Created with JetBrains WebStorm.
 * User: jiang
 * Date: 13-6-16
 * Time: 下午9:23
 * To change this template use File | Settings | File Templates.
 */
var  mutex_input_dblclick=0
$(function(){
    mutex_input_dblclick = 0;
    var timer=null;
    $(".icd-element-name").live("dblclick",function(){
        if(mutex_input_dblclick) return;
        else mutex_input_dblclick=1;

        clearTimeout(timer);
        $(this).hide();
        $(this).parent().children(".icd-element-input").show();
    });

    $(".icd-element-name").live("click",function(){
        if(mutex_input_dblclick) return;

        clearTimeout(timer);
        var that = $(this);
        timer = setTimeout(function(){
            var lable = $(that).parent().children("label").text();
            switch(lable){
                case "类名":
                    clickOnClass($(that));
                    break;
                case "属性":
                    clickOnAttribute($(that));
                    break;
            }
        },300);
    });

//    $(".icd-element-input-submit").live("click",function(){
//        var old = $(this).parent().parent().children("input[type='text']").val();
//        //if(old === '') return;
//        if(old.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
//        $(this).parent().parent().parent().children("p").text(old);
//        $(this).parent().parent().hide();
//        $(this).parent().parent().parent().children(".icd-element-name").show();
//        mutex_input_dblclick = 0;
//    });

    $(".icd-element-input-cancel").live("click",function(){
        var old = $(this).parent().parent().parent().children("p").text();
        if(old.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        $(this).parent().parent().children("input[type='text']").val(old);

        $(this).parent().parent().hide();
        $(this).parent().parent().parent().children(".icd-element-name").show();
        mutex_input_dblclick = 0;
    });

    $(".icd-element-input-delete").live("click",function(){
        $(this).parent().parent().parent().remove();
        mutex_input_dblclick = 0;
    });

    $(".icd-add").bind("click",function(){
        if(mutex_input_dblclick)   return;
        var that = this;
        var val = $(this).attr("name");
        $.ajax({
            url: '/post/html/icd-element',
            type: 'post',
            data: {lable: val},
            success: function(html)
            {
                $(that).parent().children(".element").append(html);
                mutex_input_dblclick=1;
                that = $(that).parent().children(".element").children().last();

                $(that).children(".icd-element-name").hide();
                $(that).children(".icd-element-input").show();
            }
        });
    });

    function clickOnClass(that){
        $("#cd-level2").children().remove();
        $("#cd-level3").children().remove();
        $.ajax({
            url: '/post/html/icd-span-class',
            type: 'post',
            data: {lable: ''},
            success: function(html)
            {
                $("#cd-level2").append(html);
            }
        });
    }

    function clickOnAttribute(that){
        $("#cd-level3").children().remove();
        $.ajax({
            url: '/post/html/icd-span-attr',
            type: 'post',
            data: {lable: ''},
            success: function(html)
            {
                $("#cd-level3").append(html);
            }
        });
    }


    $(".icd-element-relation-name").live("dblclick",function(){
        if(mutex_input_dblclick) return;
        else mutex_input_dblclick=1;

        clearTimeout(timer);
        $(this).parent().children().hide();
        $(this).parent().children(".icd-element-input").show();
    });

    $(".icd-element-relation-name").live("click",function(){
        if(mutex_input_dblclick) return;
        clearTimeout(timer);
        var that = $(this);
        timer = setTimeout(function(){
            var lable = $(that).parent().children("label").text();

            $("#cd-level2").children().remove();
            $("#cd-level3").children().remove();
            $.ajax({
                url: '/post/html/icd-span-relation',
                type: 'post',
                data: {lable: ''},
                success: function(html)
                {
                    $("#cd-level2").append(html);
                }
            });
        },300);
    });

    $(".icd-element-relation-input-submit").live("click",function(){
        var class1= $(this).parent().parent().children(".icd-element-input").children(".input-1").val();
        var class2= $(this).parent().parent().children(".icd-element-input").children(".input-2").val();
        if(class1.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        if(class2.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况

        var relationname = class1 +' - '+class2;
        $(this).parent().parent().parent().children("p").text(relationname);

        $(this).parent().parent().parent().children().show();
        $(this).parent().parent().hide();
        mutex_input_dblclick = 0;
    });

    $(".icd-element-relation-input-cancel").live("click",function(){
        var relationname = $(this).parent().parent().parent().children("p").text();
        relationname = relationname.split(" - ");
        if(relationname[0].replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        if(relationname[1].replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况

        $(this).parent().parent().children(".icd-element-input").children(".input-1").val(relationname[0]);
        $(this).parent().parent().children(".icd-element-input").children(".input-2").val(relationname[1]);

        $(this).parent().parent().parent().children().show();
        $(this).parent().parent().hide();

        mutex_input_dblclick = 0;
    });

    $(".icd-element-relation-input-delete").live("click",function(){
        $(this).parent().parent().parent().remove();
        mutex_input_dblclick = 0;
    });

    $(".icd-relation-add").bind("click",function(){
        if(mutex_input_dblclick)   return;
        var that = this;
        var val = $(this).attr("name");
        $.ajax({
            url: '/post/html/icd-element-double',
            type: 'post',
            data: {lable: val},
            success: function(html){
                $(that).parent().children(".element").append(html);
                mutex_input_dblclick=1;
                that = $(that).parent().children(".element").children().last();

                $(that).children().hide();
                $(that).children(".icd-element-input").show();
            }
        });
    });

    /**
     * icd-element-textarea
     */

    $(".icd-element-textarea").live("dblclick",function(){
        if(mutex_input_dblclick) return;
        else mutex_input_dblclick=1;

        clearTimeout(timer);

        $(this).hide();
        $(this).parent().children(".controls-small").show();
    });

    $(".icd-element-textarea-submit").live("click",function(){
        var old = $(this).parent().parent().children("textarea").val();
        //if(old === '') return;
        if(old.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        $(this).parent().parent().parent().children("p").text(old);

        $(this).parent().parent().hide();
        $(this).parent().parent().parent().children(".icd-element-textarea").show();
        mutex_input_dblclick = 0;
    });

    $(".icd-element-textarea-cancel").live("click",function(){
        var old = $(this).parent().parent().parent().children("p").text();
        if(old.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        $(this).parent().parent().children("textarea").val(old);

        $(this).parent().parent().hide();
        $(this).parent().parent().parent().children(".icd-element-textarea").show();
        mutex_input_dblclick = 0;
    });
});